﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Global;

namespace Helper
{
    public static class RegistryHelper
    {
        public static void InitTheRegistry()
        {
            //Load settings from registry
            try
            {
                var regMySoft = Utility.CreateRegistryKey();
                if (regMySoft != null)
                {
                    using (var tempKey = regMySoft.OpenSubKey(Constant.REGISTRY.SETTINGS))
                    {
                        if (tempKey != null)
                        {
                            Settings.IS_VOCABULARY = (tempKey.GetValue(Constant.REGISTRY.VOCABULARY).ToString() == "1");
                            Settings.IS_NOTE = (tempKey.GetValue(Constant.REGISTRY.NOTE).ToString() == "1");
                            Settings.DISPLAY_TIME =
                                Int32.Parse((string)tempKey.GetValue(Constant.REGISTRY.DISPLAY_TIME));
                            Settings.WAITING_TIME =
                                Int32.Parse((string)tempKey.GetValue(Constant.REGISTRY.WAITING_TIME));
                            Settings.BEFORE_DATE = tempKey.GetValue(Constant.REGISTRY.BEFORE_DATE).ToString();
                            Settings.AFTER_DATE = tempKey.GetValue(Constant.REGISTRY.AFTER_DATE).ToString();
                            Settings.IS_BEFORE_DATE_CHECKED =
                                (tempKey.GetValue(Constant.REGISTRY.IS_BEFORE_DATE_CHECKED).ToString() == "1");
                            Settings.IS_AFTER_DATE_CHECKED =
                                (tempKey.GetValue(Constant.REGISTRY.IS_AFTER_DATE_CHECKED).ToString() == "1");

                            //check the two checkbox
                            if (Settings.IS_BEFORE_DATE_CHECKED == true && Settings.IS_AFTER_DATE_CHECKED == true)
                            {
                                //use the default settings
                                Settings.IS_BEFORE_DATE_CHECKED = Default.SETTINGS.IS_BEFORE_DATE_CHECKED;
                                Settings.IS_AFTER_DATE_CHECKED = Default.SETTINGS.IS_AFTER_DATE_CHECKED;

                                //change the registry as default
                                tempKey.SetValue(Constant.REGISTRY.IS_BEFORE_DATE_CHECKED, "0");
                                tempKey.SetValue(Constant.REGISTRY.IS_AFTER_DATE_CHECKED, "0");
                            }

                            //check the current file name
                            Settings.CURRENT_FILE_PATH =
                                tempKey.GetValue(Constant.REGISTRY.CURRENT_FILE_PATH).ToString();
                            Settings.CURRENT_FILE_NAME =
                                tempKey.GetValue(Constant.REGISTRY.CURRENT_FILE_NAME).ToString();

                            tempKey.Close();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Default.ReverseResultToDefault();
            }
        }
    }
}
